﻿using Caliburn.Micro;
using Dicom;
using Dicom.Imaging;
using ImageProcessor;
using ImageProcessor.Imaging.Formats;
using SplitMergePanel.Models;
using SplitMergePanel.Tools;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media.Imaging;

namespace SplitMergePanel.ViewModels
{
    class ShellViewModel : Caliburn.Micro.Screen, IShell
    {
        #region 自定义变量
        const int _Width = 800;
        const int _Height = 600;
        const float fileWidth = 800;
        const float fileHeight = 970;
        int columnNum = 0;
        int rowNum = 0;
        int index = 10;
        BindableCollection<CanvasAlbum> Albums;
        int albumIndex = 1;
        int rotateDegree = 90;
        #endregion

        #region 公开属性
        private BindableCollection<CanvasData> canvasList;

        public BindableCollection<CanvasData> CanvasList
        {
            get { return canvasList; }
            set { canvasList = value; }
        }

        private CanvasAlbum album;

        public CanvasAlbum Album
        {
            get { return album; }
            set { album = value; }
        }

        public System.Windows.Media.Stretch layout;

        public System.Windows.Media.Stretch Layout
        {
            get { return layout; }
            set
            {
                this.layout = value;
                NotifyOfPropertyChange(nameof(Layout));
            }
        }

        public Dictionary<Guid, BitmapImage> DicImageList { get; set; }

        private Dictionary<string, bool> dicDegree;

        public Dictionary<string, bool> DicDegree
        {
            get { return dicDegree; }
            set { dicDegree = value; NotifyOfPropertyChange(nameof(TenDegreeEnabled)); }
        }
        public bool TenDegreeEnabled
        {
            get
            {
                return DicDegree["10"];
            }
        }
        public bool ThirtyDegreeEnabled
        {
            get
            {
                return DicDegree["30"];
            }
        }

        public bool NinetyDegreeEnabled
        {
            get { return DicDegree["90"]; }
        }
        #endregion

        public ShellViewModel()
        {
            CanvasList = new BindableCollection<CanvasData>();
            DicImageList = new Dictionary<Guid, BitmapImage>();
            CanvasList.Add(new CanvasData
            {
                Left = 0,
                Top = 0,
                CanvasHeight = _Height,
                CanvasWidth = _Width,
                IsSelected = true
            });
            DicDegree = new Dictionary<string, bool>
            {
                {"10",true },
                {"30",true },
                {"90",true },
            };
            //Albums = new BindableCollection<CanvasAlbum>();
            //Albums.Add(new CanvasAlbum());
            //Album = Albums[0];
        }


        #region Command
        public void OpenDCM()
        {
            OpenFileDialog ofg = new OpenFileDialog();
            if (ofg.ShowDialog() == DialogResult.OK)
            {
                string path = ofg.FileName;
                var image = new DicomImage(path);
                Bitmap map = image.RenderImage().AsSharedBitmap();

                DrawTags(ref map, path);
                OnePlusOne();
                CanvasList.Add(new CanvasData
                {
                    Left = 0,
                    Top = 0,
                    ImgData = ImageOp.BitmapToBitmapImage(map),
                    CanvasHeight = _Height,
                    CanvasWidth = _Width
                });
            }
        }



        //反转
        public void Invert()
        {
            if (!CanvasList.Any(i => i.IsSelected == true))
            {
                return;
            }
            var bmpimg = CanvasList.Where(i => i.IsSelected == true).First();
            Task<Bitmap>.Run(() =>
            {
                Bitmap bmp = ImageOp.BitmapImageToBitmap(bmpimg.ImgData);
                for (int i = 0; i < bmp.Width; i++)
                {
                    for (int j = 0; j < bmp.Height; j++)
                    {
                        //获取该点的像素的RGB的颜色
                        Color color = bmp.GetPixel(i, j);
                        Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);
                        bmp.SetPixel(i, j, newColor);
                    }
                }
                return bmp;
            }).ContinueWith((p) =>
            {
                System.Windows.Application.Current.Dispatcher.BeginInvoke(new System.Action(() =>
                {
                    bmpimg.ImgData = ImageOp.BitmapToBitmapImage(p.Result);
                }));
            });
        }

        public void ParallelInvert()
        {
            if (!CanvasList.Any(i => i.IsSelected == true))
            {
                return;
            }
            var canvasData = CanvasList.Where(i => i.IsSelected == true).First();
            //BitmapImage map = canvasData.ImgData;
            //Bitmap bit = ImageOp.BitmapImageToBitmap(map);
            string s = @"D:\Images\scratches.bmp";
            Bitmap bit = new Bitmap(s).Clone(new Rectangle(0, 0, 1920, 1080), PixelFormat.Format24bppRgb);
            BitmapData bitData = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            Parallel.ForEach(Partitioner.Create(0, bitData.Height), new ParallelOptions { MaxDegreeOfParallelism = 32 }, (H) =>
               {
                   unsafe
                   {
                       int X, Y, Width, Height, Stride;
                       byte* Scan0, CurP;
                       Width = bitData.Width;
                       Height = bitData.Height;
                       Stride = bitData.Stride;
                       Scan0 = (byte*)bitData.Scan0;
                       for (Y = H.Item1; Y < H.Item2; Y++)
                       {
                           CurP = Scan0 + Y * Stride;
                           for (X = 0; X < Width; X++)
                           {
                               *CurP = (byte)(255 - *CurP);
                               *(CurP + 1) = (byte)(255 - *(CurP + 1));
                               *(CurP + 2) = (byte)(255 - *(CurP + 2));
                               CurP += 3;
                           }
                       }
                   }
               });

            bit.UnlockBits(bitData);
            canvasData.ImgData = ImageOp.BitmapToBitmapImage(bit);
        }

        //灰度处理
        public void Gray()
        {
            var canvas = CanvasList.Where(i => i.IsSelected == true).First();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Task<Bitmap>.Run(() =>
            {
                Bitmap bitMap = ImageOp.BitmapImageToBitmap(canvas.ImgData);
                for (int i = 0; i < bitMap.Width; i++)
                {
                    for (int j = 0; j < bitMap.Height; j++)
                    {
                        Color origalColor = bitMap.GetPixel(i, j);
                        int grayScale = (int)(origalColor.R * .3 + origalColor.G * .59 + origalColor.B * .11);
                        Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);
                        bitMap.SetPixel(i, j, newColor);
                    }
                }
                return bitMap;
            }).ContinueWith((p) =>
            {
                System.Windows.Application.Current.Dispatcher.BeginInvoke(new System.Action(() =>
                {
                    canvas.ImgData = ImageOp.BitmapToBitmapImage(p.Result);
                }));
                sw.Stop();
                Debug.WriteLine("普通方法：" + sw.Elapsed.Ticks.ToString());
            });
        }

        /// <summary>
        /// 灰度化（非托管操作）
        /// </summary>
        public void GrayUnsafe()
        {
            var canvas = CanvasList.Where(i => i.IsSelected == true).First();
            Bitmap bitMap = ImageOp.BitmapImageToBitmap(canvas.ImgData);
            BitmapData datas = bitMap.LockBits(new Rectangle(0, 0, bitMap.Width, bitMap.Height),
        System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            unsafe
            {

                byte* p = (byte*)datas.Scan0;
                int indx = 0;
                for (int i = 0; i < bitMap.Height; i++)
                {
                    for (int j = 0; j < bitMap.Width; j++)
                    {
                        byte b, g, r;
                        b = p[indx + 1];
                        g = p[indx + 2];
                        r = p[indx + 3];
                        //byte lightLv = (byte)(r * 0.3 + g * 0.59 + b * 0.11);
                        byte gray = (byte)((r + g + b) / 3);
                        p[indx++] = gray;
                        p[indx++] = gray;
                        p[indx++] = gray;
                    }
                }
            }
            bitMap.UnlockBits(datas);
            canvas.ImgData = ImageOp.BitmapToBitmapImage(bitMap);
        }

        /// <summary>
        /// 并行图像处理
        /// </summary>
        public void ParallelGray()
        {
            Stopwatch sw = new Stopwatch();
            if (!CanvasList.Any(i => i.IsSelected == true))
            {
                return;
            }
            var canvasData = CanvasList.Where(i => i.IsSelected == true).First();
            //BitmapImage map = canvasData.ImgData;
            //Bitmap bit = ImageOp.BitmapImageToBitmap(map);
            string s = @"D:\Images\scratches.bmp";
            Bitmap bit = new Bitmap(s).Clone(new Rectangle(0, 0, 1920, 1080), PixelFormat.Format24bppRgb);
            if (bit.PixelFormat == PixelFormat.Format24bppRgb)
            {
                
                BitmapData bitData = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                sw.Start();
                Parallel.ForEach(Partitioner.Create(0,bit.Height),new ParallelOptions { MaxDegreeOfParallelism = 32},(H)=>
                {
                    unsafe
                    {
                        int X, Y, Height, Width, Stride;
                        double gray = 0;
                        byte* Scan0, CurP;
                        Width = bitData.Width;
                        Height = bitData.Height;
                        Stride = bitData.Stride;
                        Scan0 = (byte*)bitData.Scan0;
                        for (Y = H.Item1; Y < H.Item2; Y++)
                        {
                            CurP = Scan0 + Y * Stride;
                            for (X = 0; X < Width; X++)
                            {
                                gray = 0.3 * CurP[2] + 0.59 * CurP[1] + 0.11 * CurP[1];
                                CurP[2] = CurP[1] = CurP[0] = (byte)gray;
                                CurP += 3;
                            }
                        }
                    }
                });
                bit.UnlockBits(bitData);
                canvasData.ImgData = ImageOp.BitmapToBitmapImage(bit);
                sw.Stop();
                Debug.WriteLine("指针法耗时：" + sw.Elapsed.Ticks.ToString());
            }

        }


        public void BMP32To24()
        {
            var canvas = CanvasList.Where(i => i.IsSelected == true).First();
            Bitmap bitMap = ImageOp.BitmapImageToBitmap(canvas.ImgData);
            string currentpath = AppDomain.CurrentDomain.BaseDirectory + "RED.bmp";
            ImageOp.Bit32To24(bitMap, currentpath);

        }
        //导出为DCM文件
        public void ExportToDCM()
        {

        }
        public void OnePlusOne()
        {
            CanvasList.Clear();
            CanvasList.Add(new CanvasData
            {
                CanvasHeight = _Height,
                CanvasWidth = _Width,
                Left = 0,
                Top = 0,
            });
        }

        public void OnePlusTwo()
        {
            CanvasList.Clear();
            CanvasList.Add(new CanvasData
            {
                CanvasHeight = Convert.ToInt16(0.5 * _Height),
                CanvasWidth = _Width,
                Top = 0,
                Left = 0
            });
            CanvasList.Add(new CanvasData
            {
                CanvasHeight = Convert.ToInt16(0.5 * _Height),
                CanvasWidth = Convert.ToInt16(0.5 * _Width),
                Top = Convert.ToInt16(0.5 * _Height),
                Left = 0
            });
            CanvasList.Add(new CanvasData
            {
                CanvasHeight = Convert.ToInt16(0.5 * _Height),
                CanvasWidth = Convert.ToInt16(0.5 * _Width),
                Top = Convert.ToInt16(0.5 * _Height),
                Left = Convert.ToInt16(0.5 * _Width)
            });
        }

        public void TwoPlusTwo()
        {
            CanvasList.Clear();
            CanvasList.Add(new CanvasData
            {
                CanvasHeight = Convert.ToInt16(0.5 * _Height),
                CanvasWidth = Convert.ToInt16(0.5 * _Width),
                Top = 0,
                Left = 0
            });
            CanvasList.Add(new CanvasData
            {
                CanvasHeight = Convert.ToInt16(0.5 * _Height),
                CanvasWidth = Convert.ToInt16(0.5 * _Width),
                Top = 0,
                Left = Convert.ToInt16(0.5 * _Width)
            });
            CanvasList.Add(new CanvasData
            {
                CanvasHeight = Convert.ToInt16(0.5 * _Height),
                CanvasWidth = Convert.ToInt16(0.5 * _Width),
                Top = Convert.ToInt16(0.5 * _Height),
                Left = 0
            });
            CanvasList.Add(new CanvasData
            {
                CanvasHeight = Convert.ToInt16(0.5 * _Height),
                CanvasWidth = Convert.ToInt16(0.5 * _Width),
                Top = Convert.ToInt16(0.5 * _Height),
                Left = Convert.ToInt16(0.5 * _Width)
            });
        }

        public void ThreePlusThree()
        {
            for (int i = 0; i <= 2; i++)
            {
                for (int j = 0; j <= 2; j++)
                {
                    CanvasList.Add(new CanvasData
                    {
                        CanvasWidth = Convert.ToInt16(0.333 * _Width),
                        CanvasHeight = Convert.ToInt16(0.333 * _Height),
                        Left = Convert.ToInt16(0.333 * _Width) * j,
                        Top = Convert.ToInt16(0.333 * _Height) * i
                    });
                }
            }
        }

        //上下分离
        public void UpDownSplit()
        {
            if (!CanvasList.Where(i => i.IsSelected == true).Any() || CanvasList.Where(i => i.IsSelected == true).ToList().Count > 1) return;
            var selectedOne = CanvasList.FirstOrDefault(i => i.IsSelected == true);
            if (selectedOne != null)
            {
                int top = selectedOne.Top;
                int left = selectedOne.Left;
                int w = selectedOne.CanvasWidth;
                int h = selectedOne.CanvasHeight;

                selectedOne.CanvasHeight = Convert.ToInt32(0.5 * h);

                CanvasList.Add(new CanvasData
                {
                    Top = top + Convert.ToInt32(0.5 * h),
                    Left = left,
                    CanvasHeight = Convert.ToInt32(0.5 * h),
                    CanvasWidth = w,
                });
            }
        }
        //左右分离
        public void LeftRightSplit()
        {
            if (!CanvasList.Where(i => i.IsSelected == true).Any() || CanvasList.Where(i => i.IsSelected == true).ToList().Count > 1) return;
            CanvasData selectedOne = CanvasList.Where(i => i.IsSelected == true).ToList()[0];
            if (selectedOne != null)
            {
                int top = selectedOne.Top;
                int left = selectedOne.Left;
                int w = selectedOne.CanvasWidth;
                int h = selectedOne.CanvasHeight;

                selectedOne.CanvasWidth = Convert.ToInt16(0.5 * w);
                CanvasList.Add(new CanvasData
                {
                    Top = top,
                    Left = left + Convert.ToInt16(0.5 * w),
                    CanvasHeight = h,
                    CanvasWidth = Convert.ToInt16(0.5 * w),
                });
            }
        }
        //合并（仅限相邻的矩形块）
        public void Merge()
        {
            ////选择区块大于二则无效
            //if (CanvasList.Where(i => i.IsSelected == true).ToList().Count != 2) return;



            var selecteds = CanvasList.Where(i => i.IsSelected == true).ToList();

            if (selecteds.Count < 2) return;

            //两个区块都有图像无效
            if (selecteds.Where(i => i.ImgData != null).ToList().Count() > 1) return;
            //上下合并
            if (selecteds[0].CanvasHeight == selecteds[1].CanvasHeight
                && selecteds[0].CanvasWidth == selecteds[1].CanvasWidth
                && selecteds[0].Left == selecteds[1].Left)
            {
                //有图情况下
                if (selecteds.Any(i => i.ImgData != null))
                {
                    var imageOne = selecteds.Where(i => i.ImgData != null).First();
                    var emptyOne = selecteds.Where(i => i.ImgData == null).First();
                    if (imageOne.Top < emptyOne.Top)
                    {
                        imageOne.CanvasHeight += emptyOne.CanvasHeight;
                    }
                    else
                    {
                        imageOne.Top -= emptyOne.CanvasHeight;
                        imageOne.CanvasHeight += emptyOne.CanvasHeight;
                    }
                    CanvasList.Remove(emptyOne);
                }
                else
                {
                    var higherOne = selecteds[0].Top > selecteds[1].Top ? selecteds[1] : selecteds[0];
                    var deleteOne = selecteds[0].Top > selecteds[1].Top ? selecteds[0] : selecteds[1];
                    higherOne.CanvasHeight = 2 * higherOne.CanvasHeight;
                    CanvasList.Remove(deleteOne);
                }
            }
            //左右合并
            else if (selecteds[0].CanvasHeight == selecteds[1].CanvasHeight
                && selecteds[0].CanvasWidth == selecteds[1].CanvasWidth
                && selecteds[0].Top == selecteds[1].Top)
            {
                //有图情况下
                if (selecteds.Any(i => i.ImgData != null))
                {
                    var imageOne = selecteds.Where(i => i.ImgData != null).First();
                    var emptyOne = selecteds.Where(i => i.ImgData == null).First();
                    if (imageOne.Left < emptyOne.Left)
                    {
                        imageOne.Left -= emptyOne.CanvasWidth;
                        imageOne.CanvasWidth += emptyOne.CanvasWidth;
                    }
                    else
                    {
                        imageOne.CanvasWidth += emptyOne.CanvasWidth;
                    }
                    CanvasList.Remove(emptyOne);
                }
                //无图
                else
                {
                    var leftOne = selecteds[0].Left > selecteds[1].Left ? selecteds[1] : selecteds[0];
                    var deeteOne = selecteds[0].Left > selecteds[1].Left ? selecteds[0] : selecteds[1];
                    leftOne.CanvasWidth = 2 * leftOne.CanvasWidth;
                    CanvasList.Remove(deeteOne);
                }
            }
        }

        public void OnPictureClick(CanvasData data)
        {
            data.IsSelected = !data.IsSelected;

        }

        public void Export()
        {
            Bitmap expImg = new Bitmap((int)fileWidth, (int)fileHeight);
            Graphics g = Graphics.FromImage(expImg);
            g.Clear(Color.Black);
            double wideScale = fileWidth / (float)_Width;
            double heightScale = fileHeight / (float)_Height;
            List<CanvasData> listImg = CanvasList.Where(i => i.ImgData != null).ToList();
            foreach (var item in listImg)
            {
                Bitmap destImg = ImageOp.ResizeImage(ImageOp.BitmapImageToBitmap(item.ImgData), Convert.ToInt16(item.CanvasWidth * wideScale), Convert.ToInt16(item.CanvasHeight * heightScale));
                g.DrawImage(destImg, Convert.ToInt16(item.Left * wideScale), Convert.ToInt16(item.Top * heightScale), Convert.ToInt16(item.CanvasWidth * wideScale), Convert.ToInt16(item.CanvasHeight * heightScale));
            }
            expImg.Save("expImg.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            g.Dispose();
            expImg.Dispose();
        }

        //打开文件夹，并将文件
        public void OpenFiles()
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                string[] infos = Directory.GetFiles(fbd.SelectedPath);
                string[] imgInfos = infos.Where(i => i.EndsWith(".jpg") || i.EndsWith(".png")).ToArray();
                List<BitmapImage> images = infos.Select(i => ImageOp.ReadToBitMapImg(i, UriKind.RelativeOrAbsolute)).ToList();
                int imageCount = infos.Count();
                double lineNum = Math.Sqrt(imageCount);
                double lineFloor = Math.Ceiling(lineNum);

                if ((Math.Pow(lineFloor, 2) - imageCount == 0) || (Math.Pow(lineFloor, 2) - imageCount > 0 && Math.Pow(lineFloor, 2) - imageCount < lineFloor))
                {
                    rowNum = columnNum = Convert.ToInt16(lineFloor);
                }
                else if (Math.Pow(lineFloor, 2) - imageCount > lineFloor)
                {
                    rowNum = Convert.ToInt16(lineFloor);
                    columnNum = Convert.ToInt16(lineFloor - 1);
                }

                CanvasList.Clear();
                int width = _Width / columnNum;
                int Height = _Height / rowNum;
                for (int index = 0; index < columnNum * rowNum; index++)
                {
                    double row = Math.Floor((double)index / columnNum);
                    double column = index % columnNum;
                    if (index < imageCount)
                    {
                        Guid id = Guid.NewGuid();
                        var data = new CanvasData
                        {
                            CanvasHeight = Height,
                            CanvasWidth = width,
                            Left = Convert.ToInt16(width * column),
                            Top = Convert.ToInt16(Height * row),
                            ImgData = images[index],
                            ID = id
                        };
                        CanvasList.Add(data);
                        DicImageList.Add(id, images[index].Clone());
                    }
                    else
                    {
                        CanvasList.Add(new CanvasData
                        {
                            CanvasHeight = Height,
                            CanvasWidth = width,
                            Left = Convert.ToInt16(width * column),
                            Top = Convert.ToInt16(Height * row),
                        });
                    }
                }
            }
        }

        public void OnDrop(object sender, System.Windows.DragEventArgs e)
        {
            var one = CanvasList.Where(i => i.ImgData != null).ToList();
            //仅支持文件的拖放
            if (!e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
            {
                return;
            }

            string[] files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
            Bitmap map = new Bitmap(files[0]).Clone(new Rectangle(0,0,1920,1080),PixelFormat.Format24bppRgb);
            BitmapImage img = ImageOp.BitmapToBitmapImage(map);
            //BitmapImage img = ImageOp.ReadToBitMapImg(files[0], UriKind.RelativeOrAbsolute);
            index++;
            (sender as CanvasData).ImgData = img;
            Guid id = Guid.NewGuid();
            (sender as CanvasData).ID = id;
            DicImageList.Add(id, img.Clone());
        }

        public void DeleteImage(CanvasData data)
        {
            data.ImgData = null;
        }

        public Bitmap ReadImage(string path)
        {
            Bitmap img = ImageOp.GetImage(path, new System.Drawing.Size(0, 0));
            return img;
        }

        public void RotateLeft()
        {
            if (CanvasList.Where(i => i.IsSelected).ToList().Count != 1) return;
            var selected = CanvasList.Where(i => i.IsSelected).First();
            ISupportedImageFormat format = new BitmapFormat { Quality = 100 };
            using (MemoryStream outStream = new MemoryStream())
            {
                using (ImageFactory factory = new ImageFactory())
                {
                    var bitmap = ImageOp.BitmapImageToBitmap(selected.ImgData);
                    factory.Load(bitmap)
                           .Rotate(rotateDegree)
                           .Save(outStream);
                    Bitmap map = new Bitmap(outStream);
                    selected.ImgData = ImageOp.BitmapToBitmapImage(map);
                }
            }
        }

        public void RotateRight()
        {
            if (CanvasList.Where(i => i.IsSelected).ToList().Count != 1) return;
            var selected = CanvasList.Where(i => i.IsSelected).First();
            System.Drawing.Size size = new System.Drawing.Size(selected.CanvasWidth, selected.CanvasHeight);
            ISupportedImageFormat format = new BitmapFormat { Quality = 100 };
            using (MemoryStream outStream = new MemoryStream())
            {
                using (ImageFactory factory = new ImageFactory())
                {
                    var bitmap = ImageOp.BitmapImageToBitmap(selected.ImgData);
                    factory.Load(bitmap)
                           .Rotate(-rotateDegree)
                           .Save(outStream);
                    Bitmap map = new Bitmap(outStream);
                    selected.ImgData = ImageOp.BitmapToBitmapImage(map);
                }
            }
        }

        public void Reset()
        {
            if (!CanvasList.Any(i => i.IsSelected == true))
            {
                return;
            }
            var selectedOne = CanvasList.First(i => i.IsSelected == true);
            selectedOne.ImgData = DicImageList.First(dic => dic.Key == selectedOne.ID).Value;
        }


        public void ChangeLayout(string action)
        {
            switch (action)
            {
                case "Fill":
                    Layout = System.Windows.Media.Stretch.Fill;
                    break;
                case "Uniform":
                    Layout = System.Windows.Media.Stretch.Uniform;
                    break;
                case "UniformToFill":
                    Layout = System.Windows.Media.Stretch.UniformToFill;
                    break;
                default:
                    break;
            }
            CanvasList.Apply(i => { i.ImgData = null; });
            foreach (var item in CanvasList)
            {
                if (DicImageList.Any(i => i.Key == item.ID))
                {
                    item.ImgData = DicImageList.Where(i => i.Key == item.ID).First().Value;
                }
            }
        }

        public void ChangeDegree(System.Windows.Controls.MenuItem sender, string degree)
        {
            sender.IsChecked = !sender.IsChecked;
            rotateDegree = Convert.ToInt16(degree);
            //TODO:bug remain
            //DicDegree.Where(i => i.Key != degree).Apply(i => i.Value = false);
        }

        #endregion

        #region 分页相关
        public void NextPage()
        {

        }

        public void PreviousPage()
        {

        }

        public void AddPage()
        {
            albumIndex++;
            Albums.Add(new CanvasAlbum()
            {
                PageIndex = albumIndex,
                Images = new BindableCollection<CanvasData>()
                {
                   new CanvasData{CanvasHeight = _Height,CanvasWidth = _Width,Left = 0,Top = 0 }
                }
            });
            //Album = Albums.
        }
        #endregion

        #region 其他方法
        //显示Tags
        private void DrawTags(ref Bitmap map, string path)
        {
            var file = DicomFile.Open(path);
            string patientName = file.Dataset.GetValue<string>(DicomTag.PatientName, 0);
            //string date = file.Dataset.GetValue<string>(DicomTag.Date, 0);
            string winwidth = file.Dataset.GetValue<string>(DicomTag.WindowWidth, 0);
            string winCenter = file.Dataset.GetValue<string>(DicomTag.WindowCenter, 0);
            //string checktype = file.Dataset.GetValue<string>(DicomTag.DataType, 0);
            string age = file.Dataset.GetValue<string>(DicomTag.PatientAge, 0);
            string[] arr = new string[] { patientName, winwidth, winCenter, age };
            Graphics g = Graphics.FromImage(map);
            int height = 60;
            int i = 0;
            foreach (var item in arr)
            {
                g.DrawString(item, new Font("宋体", 40), new SolidBrush(Color.White), 5, height * i);
                i++;
            }
        }
        #endregion
    }
}
